﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Profile_Management.Setup
{
    public partial class MasterCategory : Form
    {
        private LibManagementEntities context;
        private enum UpdateType { New = 0, Edit = 1 }
        UpdateType eUpdateType;
        private Category OCategory;
        public MasterCategory()
        {
            InitializeComponent();
            context = new LibManagementEntities();
            OCategory = new Category();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            btnEdit.Enabled = false;
            txtCategoryName.Enabled = true;
            nudBookLimit.Enabled = true;
            nudDueDate.Enabled = true;
            nudFinePerDay.Enabled = true;
            lsbCategory.Enabled = false;
            btnSave.Enabled = true;
            eUpdateType = UpdateType.New;
            txtCategoryName.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                context = new LibManagementEntities();
                btnNew.Enabled = true;
                btnEdit.Enabled = true;
                lsbCategory.Enabled = true;
                if (eUpdateType == UpdateType.New)
                {
                    OCategory = new Category();
                    OCategory.CategoryName = txtCategoryName.Text;
                    OCategory.BookLimit = nudBookLimit.Value;
                    OCategory.DueDate = nudDueDate.Value;
                    OCategory.FinePerDay = nudFinePerDay.Value;
                    context.Categories.Add(OCategory);
                }
                else if (eUpdateType == UpdateType.Edit)
                {
                    OCategory = context.Categories.Single(x => x.CategoryId == (decimal)lsbCategory.SelectedValue);
                    OCategory.CategoryName = txtCategoryName.Text;
                    OCategory.DueDate = nudDueDate.Value;
                    OCategory.FinePerDay = nudFinePerDay.Value;
                    OCategory.BookLimit = nudBookLimit.Value;
                }
                context.SaveChanges();
                btnClear_Click(sender, e);
                MasterCategory_Load(sender, e);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCategoryName.Clear();
            txtCategoryName.Enabled = false;
            nudBookLimit.Value = 0;
            nudDueDate.Value = 0;
            nudFinePerDay.Value = 0;
            btnEdit.Enabled = true;
            btnSave.Enabled = true;
            btnNew.Enabled = true;
            lsbCategory.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnNew.Enabled = false;
            lsbCategory.Enabled = false;
            txtCategoryName.Enabled = true;
            nudBookLimit.Enabled = true;
            nudDueDate.Enabled = true;
            nudFinePerDay.Enabled = true;
            btnSave.Enabled = true;
            eUpdateType = UpdateType.Edit;
        }

        private void MasterCategory_Load(object sender, EventArgs e)
        {
            context = new LibManagementEntities();
            txtCategoryName.Enabled = false;
            nudBookLimit.Enabled = false;
            nudDueDate.Enabled = false;
            nudFinePerDay.Enabled = false;
            btnSave.Enabled = false;

            lsbCategory.SetDataSource(context.Categories.ToList(), "CategoryName", "CategoryId");
        }

        private void lsbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                context = new LibManagementEntities();
                OCategory = context.Categories.Single(x => x.CategoryId == (decimal)lsbCategory.SelectedValue);
                txtCategoryName.Text = OCategory.CategoryName;
                nudBookLimit.Value = OCategory.BookLimit;
                nudDueDate.Value = (decimal)OCategory.DueDate;
                nudFinePerDay.Value = (decimal)OCategory.FinePerDay;
            }
            catch (Exception)
            {
            }
        }
    }
}
